//
//  CXDestinationMoreViewController.m
//  Travel
//
//  Created by dllo on 15/12/1.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "CXDestinationMoreViewController.h"
#import "CXDestinationCollectionViewCell.h"
#import "CXNetHandle.h"
#import "CXDestinationDataModel.h"
#import "CXDestinationDetailViewController.h"
#import "CXDestinationCityListViewController.h"

@interface CXDestinationMoreViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) NSArray *dataArr;
@property (nonatomic, retain) UICollectionView *collectionView;

@end

@implementation CXDestinationMoreViewController

- (void)dealloc
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = YES;
    
    [self prepareData];
    [self createCollectionView];
    
}

- (void)prepareData
{
    NSString *str_url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/index_places/%@/", self.index];
    [CXNetHandle getUrl:str_url cookie:nil result:^(id result) {
        if (result == nil) {
            
        }else{
            [CXLoadMBProgress removeMBProgress:self.view];
            self.dataArr = [CXDestinationDataModel transformWithArray:result[@"data"]];
            [self.collectionView reloadData];
        }

    }];
    
}

- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((WIDTH - 30) / 2, (WIDTH - 30) / 2);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 20, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[CXDestinationCollectionViewCell class]forCellWithReuseIdentifier:@"collection_item"];
    
    [CXLoadMBProgress loadMBProgress:self.view];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CXDestinationCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection_item" forIndexPath:indexPath];
    item.model_destination = self.dataArr[indexPath.item];
    return item;
}

#pragma mark - 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str_valueId = [self.self.dataArr[indexPath.row] valueId] ;
    NSString *str_valueId_new = [str_valueId substringToIndex:1];
    NSInteger tag_id = [str_valueId_new integerValue];
    
    if ((tag_id > 0 && tag_id <= 9 ) || [str_valueId isEqualToString:@"HK"] || [str_valueId isEqualToString:@"MO"]) {
        CXDestinationDetailViewController *destinationDetailVC = [[CXDestinationDetailViewController alloc]init];
        destinationDetailVC.model_data = self.dataArr[indexPath.item];
        destinationDetailVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:destinationDetailVC animated:YES];
    }else{
        CXDestinationCityListViewController *cityListVC = [[CXDestinationCityListViewController alloc] init];
        cityListVC.model_data = self.dataArr[indexPath.item];
        [self.navigationController pushViewController:cityListVC animated:YES];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
